import os
import re
from django.http import HttpResponse, JsonResponse
import json
from django.views.decorators.csrf import csrf_exempt
from pymysql.converters import escape_string
from Get_movie_word.tests import get_complexity_0_word, get_movie_script, get_chinese
from SQL_CRUD_Interface.CRUD import select_interface


'''
  `english` varchar(80) DEFAULT NULL,
  `chinese` varchar(120) DEFAULT '1',
  `status` varchar(120) NOT NULL DEFAULT '1',
  `complexity` int(11) NOT NULL DEFAULT '100' COMMENT '(太简单单词=0)_(普通单词=100)_(复杂单词=200)_(特别复杂单词=300);',
  `level_2` varchar(120) NOT NULL DEFAULT '1',
'''


class word:
    index = 0
    eng = ''
    chi = ''
    eng_script = ''
    chi_script = ''

    def __init__(self, index, eng, chi, eng_script, chi_script):
        self.index = index
        self.eng = eng
        self.chi = chi
        self.eng_script = eng_script
        self.chi_script = chi_script

@csrf_exempt
def movie_word(request):
    if request.method == "GET":
        name = request.GET.get("name")
        if name is not None:
            temp = ""
            sql = '     select `english`, `chinese` from `tb_movie_script` where `movie_name` = "%s"       ' % escape_string(name)
            aaa = select_interface("127.0.0.1", "root", "root", "study_english_database", sql)
            for ii in aaa:
                ii = ii[0].replace("\n", "  --  ")
                ii = ii.lower()
                temp = temp + "  " + ii + "  "
            temp =  re.sub("[^a-zA-Z]+", "  ", temp)            # 去掉所有的非英文
            arrrr = temp.split(" ")
            newSet = set(arrrr)

            word_list = []
            complexity_0_word_list = get_complexity_0_word()

            index = -1
            for ii in newSet:
                if ii != '' and ii not in complexity_0_word_list:
                    index += 1
                    movie_script = get_movie_script(ii)
                    try:
                        eng_script = movie_script[0][0].replace("'", "`").replace("\"", "`")
                        chi_script = movie_script[0][1].replace("'", "`").replace("\"", "`")
                    except:
                        eng_script = "not found"
                        chi_script = "没有找到"
                    # ==================================================================
                    try:
                        chinese = get_chinese(ii)
                    except:
                        chinese = "没找到相关中文翻译"
                    # ==================================================================

                    newWord = word(index, ii, chinese, eng_script, chi_script)
                    # default          # python对象按照字典key:value来对应
                    # sort_keys=True   # 按顺序排列
                    # indent=4         # 格式化输出，也就是在输出前空多少格
                    # ensure_ascii=False     #json.dumps在默认情况下，对于非ascii字符生成的是相对应的字符编码，而非原始字符
                    word_list.append(newWord.__dict__)
            json_data = json.dumps(word_list, default=lambda object: object.__dict__, sort_keys=True, indent=4,ensure_ascii=False)
            return HttpResponse(json_data)